package com.nutransit;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import com.mylibrary.mylibrary.parsing.AsyncTaskInterface;
import com.mylibrary.mylibrary.parsing.AsyncTaskUtility;
import com.mylibrary.mylibrary.util.CommonCode;
import com.mylibrary.mylibrary.util.SessionData;
import com.nutransit.commoncode.Webservice_parametrs;
import com.nutransit.dialogs.Dialog_signup_with;

public class Login_Activity extends ActionBarActivity implements Webservice_parametrs
{
	private Button buttonSignup, buttonLogin;
	private EditText editUsername, editPassword;
	private RadioButton rb_User, rb_Driver;
	
	private Context context;
	private Resources resources;
	private SessionData sessionData;
	
	private String strUserName, strPassword, str_UserType;
	private HashMap<String, String> hashMap;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);

		init_var();
	}

	private void init_var()
	{
		context = Login_Activity.this;
		resources = context.getResources();
		
		sessionData = new SessionData(context, PACKAGE_NAME);
		
		buttonSignup = (Button) findViewById(R.id.buttonSignup);
		buttonLogin = (Button) findViewById(R.id.buttonLogin);
		editUsername = (EditText) findViewById(R.id.editUsername);
		editPassword = (EditText) findViewById(R.id.editPassword);
		
		rb_Driver = (RadioButton) findViewById(R.id.rb_Driver);
		rb_User = (RadioButton) findViewById(R.id.rb_User);

		buttonSignup.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				new Dialog_signup_with(Login_Activity.this, Login_Activity.this);
			}
		});

		buttonLogin.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				strUserName = editUsername.getText().toString().trim();
				strPassword = editPassword.getText().toString().trim();
						
				if(strUserName.length()==0)
				{
					CommonCode.showEditTextErrorMessage(editUsername, resources.getString(R.string.Please_enter_user_name));
				}
				else if(strPassword.length()==0)
				{
					CommonCode.showEditTextErrorMessage(editPassword, resources.getString(R.string.Please_enter_password));
				}
				else if(rb_User.isChecked()==false && rb_Driver.isChecked()==false)
				{
					CommonCode.showMessage(resources.getString(R.string.Please_select_login_type), context);
				}
				else
				{
					str_UserType =  rb_User.isChecked()?TAG_user:TAG_driver;
					hashMap = new HashMap<String, String>();
					hashMap.put(TAG_username, strUserName);
					hashMap.put(TAG_password, strPassword);
					hashMap.put(TAG_flag,str_UserType);
					new AsyncTaskUtility(context, hashMap, URL_LOGIN).getJSONResponse(new AsyncTaskInterface()
					{
						@Override
						public void getJSONObjectFromAsynkTask(JSONObject json)
						{
							try
							{
								if(json!=null)
								{
									if(json.has(TAG_message))
										CommonCode.showMessage(json.getString(TAG_message), context);
									if(json.has(TAG_status) && json.getString(TAG_status).equalsIgnoreCase(SUCCESS))
									{
										JSONObject jsonObject_data = json.getJSONArray(TAG_data).getJSONObject(0);
										MainActivity_Nutransit.jsonObject_data = jsonObject_data;
										sessionData.setString(TAG_data, ""+jsonObject_data);
										if(str_UserType.equalsIgnoreCase(TAG_user))
											sessionData.setString(TAG_user_type, TAG_user);
										else
										{
											if(jsonObject_data.getString(TAG_driver_type).equalsIgnoreCase("c"))
												sessionData.setString(TAG_user_type, TAG_cab);
											else
												sessionData.setString(TAG_user_type, TAG_van);
										}
										startActivity(new Intent(context, MainActivity_Nutransit.class));
										Login_Activity.this.finish();
									}
								}
							}
							catch (JSONException e)
							{
								e.printStackTrace();
							}
						}
					});
				}
			}
		});
	}
}
